#if 0

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

int main() {
    // 初始化视频捕获（替换为你的视频路径）
    VideoCapture cap(1);
    if (!cap.isOpened()) {
        cerr << "Error: Could not open video." << endl;
        return -1;
    }

    // 初始化背景减除器（MOG2）
    Ptr<BackgroundSubtractor> bgSubtractor = createBackgroundSubtractorMOG2(500, 16, true);
    Mat frame, fgMask, prevGray, flow;

    // 第一帧处理
    cap >> frame;
    if (frame.empty()) return -1;
    cvtColor(frame, prevGray, COLOR_BGR2GRAY);

    while (true) {
        cap >> frame;
        if (frame.empty()) break;

        // --------------------- 背景减除 ---------------------
        bgSubtractor->apply(frame, fgMask);
        // 形态学去噪
        morphologyEx(fgMask, fgMask, MORPH_OPEN, getStructuringElement(MORPH_ELLIPSE, Size(3, 3)));

        // --------------------- 稠密光流计算 ---------------------
        Mat gray;
        cvtColor(frame, gray, COLOR_BGR2GRAY);
        Mat flow(prevGray.size(), CV_32FC2); // 光流向量存储
        calcOpticalFlowFarneback(
            prevGray, gray, flow,
            0.5, 3, 15, 3, 5, 1.2, 0
            );

        // --------------------- 雨势分析 ---------------------
        int rainPixelCount = 0;
        float avgMotionX = 0, avgMotionY = 0;

        // 遍历前景区域的光流
        for (int y = 0; y < flow.rows; y += 5) { // 步长5提升效率
            for (int x = 0; x < flow.cols; x += 5) {
                if (fgMask.at<uchar>(y, x) > 128) { // 只分析前景像素
                    Point2f flowVec = flow.at<Point2f>(y, x);
                    float motionMagnitude = sqrt(flowVec.x * flowVec.x + flowVec.y * flowVec.y);

                    // 雨滴运动特性过滤（排除过大/过小运动）
                    if (motionMagnitude > 0.3 && motionMagnitude < 5.0) {
                        rainPixelCount++;
                        avgMotionX += flowVec.x;
                        avgMotionY += flowVec.y;

                        // 绘制光流箭头（绿色）
                        arrowedLine(frame,
                                    Point(x, y),
                                    Point(x + flowVec.x, y + flowVec.y),
                                    Scalar(0, 255, 0), 1);
                    }
                }
            }
        }

        // --------------------- 结果显示 ---------------------
        if (rainPixelCount > 0) {
            avgMotionX /= rainPixelCount;
            avgMotionY /= rainPixelCount;

            // 绘制平均运动方向（红色大箭头）
            Point center(frame.cols / 2, frame.rows / 2);
            arrowedLine(frame, center,
                        center + Point(avgMotionX * 10, avgMotionY * 10),
                        Scalar(0, 0, 255), 2);

            // 显示雨势强度
            putText(frame, format("Rain Intensity: %d", rainPixelCount),
                    Point(20, 30), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0, 0, 255), 2);
            putText(frame, format("Avg Direction: (%.1f, %.1f)", avgMotionX, avgMotionY),
                    Point(20, 60), FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0, 0, 255), 2);
        }

        // 显示结果
        imshow("Rain Detection", frame);
        imshow("Foreground Mask", fgMask);

        // 更新前一帧
        prevGray = gray.clone();

        if (waitKey(30) == 27) break; // ESC退出
    }

    cap.release();
    destroyAllWindows();
    return 0;
}

#endif

#if 1

#include "mainwindow.h"
#include <QApplication>
#include <QChart>
#include <QChartView>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

#endif
